﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using Cms.Utilities;
using Cms.Web.Framework.Extensions;
using Cms.Web.Framework.Plugins.Attributes;

namespace Cms.Web.Framework.Plugins
{
    public class JwPlayer : BasePlugin
    {
        // Property fields
        private string _file;
        private int _height;
        private string _id;
        private int _width;

        [Browse("media")]
        public string File
        {
            get { return _file; }
            set { _file = value; }
        }
        
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }
        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            IEnumerable<JwPlayer> existingJwPlayers = Page.AllControls().OfType<JwPlayer>();
            _id = "JwPlayer_" + existingJwPlayers.Count();
        }

        //protected override void Render(HtmlTextWriter writer)
        //{
        //    try
        //    {
        //        Page.AddHeaderScriptInclude("jwplayer/jwplayer.js");
        //        string code = "<div id=\"" + _id + "\">Loading player...</div>" +
        //                        "<script type=\"text/javascript\">// <![CDATA[" + System.Environment.NewLine +
        //                        "jwplayer(\"" + _id + "\").setup({ " +
        //                        "flashplayer: \"/jwplayer/player.swf\", " +
        //                        "file: \"/" + _file + "\", " +
        //                        "height: " + _height + ", " +
        //                        "width: " + _width + " }); " + System.Environment.NewLine +
        //                        "// ]]></script>";
        //        writer.Write(code);
        //    }
        //    catch (Exception ex)
        //    {
        //        Logging.LogError("JwPlayer", ex);
        //        this.Controls.Clear();
        //    }
        //}

        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                Page.AddHeaderScriptInclude("jwplayer/jwplayer.js");
                string code = "<div id=\"" + _id + "\">Loading player...</div>" +
                              "<script type=\"text/javascript\">// <![CDATA[" + Environment.NewLine +
                              "jwplayer(\"" + _id + "\").setup({ " +
                              "flashplayer: \"/jwplayer/player.swf\", " +
                              "file: \"/" + _file + "\", " +
                              "height: " + _height + ", " +
                              "width: " + _width + " }); " + Environment.NewLine +
                              "// ]]></script>";
                Controls.Add(new LiteralControl(code));
            }
            catch (Exception ex)
            {
                Logging.LogError("JwPlayer", ex);
                Controls.Clear();
            }
        }
    }
}